package br.com.massai.model.business;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import br.com.massai.model.dao.UnitMeasureRepository;
import br.com.massai.model.entity.UnitMeasure;
import br.com.massai.model.exceptions.ErrorCore;
import br.com.massai.model.exceptions.SystemException;

@Stateless
public class UnitMeasureFacade implements UnitMeasureBusiness {

	private static final long serialVersionUID = -7444279335741181754L;

	@EJB private UnitMeasureRepository unitMeasureRepository;

	@Override
	public UnitMeasure findID(Long id) throws SystemException {
		return unitMeasureRepository.findID(id);
	}

	@Override
	public UnitMeasure save(UnitMeasure entity) throws SystemException {
		checkBusinessRule(entity);
		if(entity.getId() == null) return unitMeasureRepository.create(entity);
		else return unitMeasureRepository.update(entity);
	}

	@Override
	public void delete(UnitMeasure entity) throws SystemException {
		unitMeasureRepository.delete(entity);
	}

	@Override
	public void checkBusinessRule(UnitMeasure entity) throws SystemException {
		if(entity == null) throw new SystemException(ErrorCore.ERROR_ILLEGAL_ARGUMENT);
		if(entity.getAcronym() == null || entity.getAcronym().isEmpty()) throw new SystemException(ErrorCore.ERROR_REQUIRED_FIELD, "Sigla");
		if(entity.getNameMeasure() == null || entity.getNameMeasure().isEmpty()) throw new SystemException(ErrorCore.ERROR_REQUIRED_FIELD, "Nome");
	}
}